package com.myBlog.service.impl;

import com.myBlog.enums.AppHttpCodeEnum;
import com.myBlog.exception.SystemException;
import com.myBlog.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("ps")
public class PermissionService {

    /**
     * 判断当前用户是否具有permission
     * @param permission 要判断的权限
     * @return
     */
    public boolean hasPermission(String permission){
        //判断当前用户是否具有permission，如果是超级管理员直接返回true
        if (SecurityUtils.isAdmin()){
            return true;
        }
        if (permission == null){
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        //否则获取当前登录用户所具有的权限，判断是否存在permission
        List<String> permissions = SecurityUtils.getLoginUser().getPermissions();
        return permissions.contains(permission);
    }
}
